package com.example.controller;


import com.example.bean.TokenBean;
import com.example.bean.UserBean;
import com.example.jwt.JwtUtils;
import com.example.jwt.PassToken;
import com.google.gson.Gson;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class UserApi {

    //登录
    @PassToken
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public String login(@RequestParam("username") String username, @RequestParam("password") String password) {
        TokenBean tokenBean = new TokenBean();
        UserBean base = new UserBean();
        base.setId("1");
        base.setUsername("hutao");
        base.setPassword("123456");
        if (username.equals(base.getUsername()) && password.equals(base.getPassword())) {
            String token = JwtUtils.getToken(base);
            tokenBean.setErrCode(0);
            tokenBean.setMessage("success");
            tokenBean.setToken(token);
            tokenBean.setUserBean(base);
        }
        else {
            tokenBean.setErrCode(1);
            tokenBean.setMessage("用户名或密码错误");
        }
        return new Gson().toJson(tokenBean);
    }

}
